{[{define "sys/user/role.html"}]}
{[{template "sys/base/mark.html"}]}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>新增页面</title>
    <link rel="stylesheet" href="/pear-admin-layui/component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <input name="id" type="hidden" value="{[{.id}]}">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">roles</label>
                <div class="layui-input-block">
                    {[{ range $i, $v := .roles }]}
                    <input type="checkbox"
                           {[{if ne $v.UserId 0}]}
                           checked
                           {[{end}]}
                           lay-filter="checkbox" value="{[{$v.RoleId}]}" title="{[{$v.RoleName}]}">
                    {[{ end }]}
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/pear-admin-layui/component/layui/layui.js"></script>
<script src="/pear-admin-layui/component/pear/pear.js"></script>
<script>

    layui.use(['form', 'jquery', 'loading', 'layer'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let loading = layui.loading;
        let layer = layui.layer;
        let id = {[{.id}]}

        let roleIds = []

        $('input[lay-filter="checkbox"]:checked').each(function () {
            console.log($(this).val())
            roleIds.push($(this).val())
        });

        form.on('checkbox(checkbox)', function (data) {
            if (data.elem.checked) {
                if (!roleIds.includes(data.value)) {
                    roleIds.push(data.value)
                }
            } else {
                if (roleIds.includes(data.value)) {
                    for (let i = 0; i < roleIds.length; i++) {
                        if (data.value === roleIds[i]) {
                            roleIds.splice(i, 1)
                        }
                    }
                }
            }
            console.log(data.elem.checked)
            console.log(data.value)
            console.log(roleIds)

            loading.Load(1, "loading data ...");
            $.post('/sys/user/role/edit', {id: id, roleId: data.value, checked: data.elem.checked}, function (r) {
                loading.loadRemove();
                if (r.code === 200) {
                    layer.msg(r.msg, {icon: 1});
                } else {
                    layer.msg(r.msg, {icon: 2});
                }
            });

        });

        form.on('submit(save)', function (data) {
            // if (roleIds.length === 0) {
            //     layer.msg("没有选中角色", {icon: 2, time: 1000})
            //     return false
            // }
            // console.log(data.field)
            // data.field.roleIds = roleIds
            // loading.Load(1, "loading data ...");
            // $.post('/sys/user/role/save', data.field, function (r) {
            //     loading.loadRemove(1000);
            //     if (r.code === 200) {
            //         layer.msg(r.msg, {icon: 1, time: 1000}, function () {
            //             parent.layer.close(parent.layer.getFrameIndex(window.name));
            //             // parent.layui.table.reload("dict-type-table");
            //         });
            //     } else {
            //         layer.msg(r.msg, {icon: 2, time: 1000});
            //     }
            // });
            parent.layer.close(parent.layer.getFrameIndex(window.name));
            return false;
        });
    })
</script>
</body>
</html>
{[{end}]}
